package com.itheima.manager_system;

import com.itheima.manager_system.domain.Person;
import com.itheima.manager_system.domain.Student;
import com.itheima.manager_system.domain.Teacher;
import com.itheima.manager_system.student_service.ScoreOperater;
import com.itheima.manager_system.student_service.StudentOperater;
import com.itheima.manager_system.teacher_service.SalaryManager;
import com.itheima.manager_system.teacher_service.TeacherOperater;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Entry {
   private static  Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws IOException {

        ArrayList<Person> studentList = new ArrayList<>();
        ArrayList<Person> teacherList = new ArrayList<>();
        Student st = new Student();
        st.setJob("student");
        st.setId(0);
        Teacher te = new Teacher();
        te.setJob("teacher");
        te.setId(0);
        studentList.add(st);
        teacherList.add(te);
        ScoreOperater scoreOperater = new ScoreOperater();
        StudentOperater studentOperater = new StudentOperater();
        TeacherOperater teacherOperater = new TeacherOperater();
        SalaryManager salaryManager = new SalaryManager();
        importPath(studentList, teacherList);

        String select;
        System.out.println("__________欢迎进入管理系统__________");//todo 增加老师管理菜单

        loop:
        while (true) {
            System.out.println("----------------------------------");
            System.out.println("1 老师管理系统 2 学生管理系统 3 退出");
            System.out.println("----------------------------------");
            select = sc.next();
            switch (select) {
                case "1":
                    loopTeacher:
                    while (true) {
                        System.out.println("----------------------------------");
                        System.out.println("1.老师信息管理  2.老师薪资管理 3. 返回上一层");
                        System.out.println("----------------------------------");
                        select = sc.next();
                        switch (select) {
                            case "1":
                                teacherOperater.InfoMenu(teacherList);
                                break;
                            case "2":
                                salaryManager.SalaryMenu(teacherList);
                                break;
                            case "3":
//                                System.out.println("谢谢使用,再见");
                                break loopTeacher;
                            default:
                                System.out.println("输入有误");
                                break;
                        }
                    }
                    break;
                case "2":
                    loopStudent:
                    while (true) {
                        System.out.println("----------------------------------");
                        System.out.println("1.学生信息管理  2.学生分数管理 3. 返回上一层");
                        System.out.println("----------------------------------");
                        select = sc.next();
                        switch (select) {
                            case "1":
                                studentOperater.infoMenu(studentList);
                                break;
                            case "2":
                                scoreOperater.scoreMenu(studentList);
                                break;
                            case "3":
//                                System.out.println("谢谢使用,再见");
                                break loopStudent;
                            default:
                                System.out.println("输入有误");
                                break;
                        }
                    }
                    break;
                case "3":
                    System.out.println("谢谢使用,再见");
                    break loop;
                default:
                    System.out.println("输入有误");
                    break;

            }
        }
        teacherOperater.sortList(teacherList);
        studentOperater.sortList(studentList);
        studentList.remove(0);
        teacherList.remove(0);

        exportPath(studentList, teacherList);


    }
    public static void exportList(ArrayList<Person> list, File f) throws IOException {
        if ("student".equals(list.get(0).getJob())) {
            BufferedWriter writer = new BufferedWriter(new FileWriter(f));
            for (Person person : list) {
                Student st = (Student) person;
                StringBuilder sb = new StringBuilder();
                sb.append(st.getId()).append(",").append(st.getName()).append(",")
                        .append(st.getAge()).append(",").append(st.getAdress()).append(",")
                        .append(st.getChinese()).append(",").append(st.getMath()).append(",")
                        .append(st.getEnglish()).append(",").append(st.SumScore()).append(",").append(st.getJob());
                writer.write(sb.toString());
                writer.newLine();
                writer.flush();
            }
            writer.close();
        }
        if ("teacher".equals(list.get(0).getJob())) {
            BufferedWriter writer = new BufferedWriter(new FileWriter(f));
            for (Person person : list) {
                Teacher st = (Teacher) person;
                StringBuilder sb = new StringBuilder();
                sb.append(st.getId()).append(",").append(st.getName()).append(",")
                        .append(st.getAge()).append(",").append(st.getAdress()).append(",").append(st.getJob()).append(",")
                        .append(st.getWorkingAge()).append(",").append(st.getSalary());
                writer.write(sb.toString());
                writer.newLine();
                writer.flush();
            }
            writer.close();
        }


    }

    public static void exportPath(ArrayList<Person> studentList, ArrayList<Person> teacherList) throws IOException {
        File studentTxt = new File("D:\\code\\student_system\\Version4\\src\\com\\itheima\\domain\\students.txt");
        if (!studentTxt.exists()) {
            studentTxt.createNewFile();
        }
        File teacherTxt = new File("D:\\code\\student_system\\Version4\\src\\com\\itheima\\domain\\teachers.txt");
        if (!teacherTxt.exists()) {
            teacherTxt.createNewFile();
        }


        exportList(studentList, studentTxt);
        exportList(teacherList, teacherTxt);
    }



    public static void importPath(ArrayList<Person> studentList, ArrayList<Person> teacherList) throws IOException {
        File studentTxt = new File("D:\\code\\student_system\\Version4\\src\\com\\itheima\\domain\\students.txt");
        if (!studentTxt.exists()) {
            studentTxt.createNewFile();
        }
        File f_teacher = new File("D:\\code\\student_system\\Version4\\src\\com\\itheima\\domain\\teachers.txt");
        if (!f_teacher.exists()) {
            f_teacher.createNewFile();
        }
        importInformation(studentList, studentTxt);//载入信息
        importInformation(teacherList, f_teacher);//载入信息

    }

    public static void importInformation(ArrayList<Person> list, File f) throws IOException {


        BufferedReader reader = new BufferedReader(new FileReader(f));
        String line;
        if ("student".equals(list.get(0).getJob())) {//todo
            while ((line = reader.readLine()) != null) {
                String[] info = line.split(",");
                //编号  姓名 年龄 地址 语文 数学 英语 总分
                Student st = new Student();
                if (info.length > 0) {
                    st.setId(Integer.parseInt(info[0]));
                }
                if (info.length > 1) {
                    st.setName(info[1]);
                }
                if (info.length > 2) {
                    st.setAge(Integer.parseInt(info[2]));
                }
                if (info.length > 3) {
                    st.setAdress(info[3]);
                }
                if (info.length > 4) {
                    st.setChinese(Integer.parseInt(info[4]));
                }
                if (info.length > 5) {
                    st.setMath(Integer.parseInt(info[5]));
                }
                if (info.length > 6) {
                    st.setEnglish(Integer.parseInt(info[6]));
                }
                if (info.length > 8) {
                    st.setJob(info[8]);
                }
                list.add(st);
            }
        }
        if ("teacher".equals(list.get(0).getJob())) {
            while ((line = reader.readLine()) != null) {
                String[] info = line.split(",");
                //编号  姓名 年龄 地址 语文 数学 英语 总分
                Teacher t = new Teacher();
                if (info.length > 0) {
                    t.setId(Integer.parseInt(info[0]));
                }
                if (info.length > 1) {
                    t.setName(info[1]);
                }
                if (info.length > 2) {
                    t.setAge(Integer.parseInt(info[2]));
                }
                if (info.length > 3) {
                    t.setAdress(info[3]);
                }

                if (info.length > 4) {
                    t.setJob(info[4]);
                }
                list.add(t);
            }
            reader.close();
        }

    }
}
